import { _decorator, Component, Label, Node } from 'cc';
import { DataHelper } from '../system/DataHelper';
import { GameData } from '../system/GameData';
import { AppControll, ConfigKey, EventName } from '../manager/AppControll';
import EvtManger from '../tools/EvtManger';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('LevelChallenge')
export class LevelChallenge extends Component {
    @property(Node)
    mNdBody: Node = null;
    @property(Node)
    mNdYesBtn: Node = null;
    @property(Node)
    mNdVideoBtn: Node = null;
    @property(Node)
    mNdTomorrowBtn: Node = null;

    private isLock = false;

    protected onLoad(): void {
        this.mNdBody = this.node.getChildByName("body");
        this.InitView();
    }

    InitView() {
        if (GameData.GetCurlevelChallengeFreeTimes() > 0) {
            let ndTimes = this.mNdYesBtn.getChildByName("spend").getChildByName("time");
            ndTimes.getComponent(Label).string = GameData.GetCurlevelChallengeFreeTimes() + "/" + DataHelper.GetConfigByKey(ConfigKey.levelChallengeFreeTimes).value;
            this.mNdYesBtn.active = true;
            this.mNdVideoBtn.active = false;
            this.mNdTomorrowBtn.active = false;
        } else if (GameData.GetCurlevelChallengeFreeTimes() <= 0 && GameData.GetCurlevelChallengeAdTimes() > 0) {
            let ndTimes = this.mNdVideoBtn.getChildByName("videoSpend").getChildByName("time");
            ndTimes.getComponent(Label).string = GameData.GetCurlevelChallengeAdTimes() + "/" + DataHelper.GetConfigByKey(ConfigKey.levelChallengeVideoTimes).value;
            this.mNdYesBtn.active = false;
            this.mNdVideoBtn.active = true;
            this.mNdTomorrowBtn.active = false;
        } else {
            this.mNdYesBtn.active = false;
            this.mNdVideoBtn.active = false;
            this.mNdTomorrowBtn.active = true;
        }
    }

    start() {

    }

    OnBtnClose() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        this.node.destroy();
    }

    OnBtnConfirm() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        GameData.playerData.challengeCount -= 1;
        EvtManger.do(EventName.levelChallenge);
        GameData.SavePlayData();
        this.node.destroy();
    }

    OnBtnVideo() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.isLock) return;
        this.isLock = true;
        this.scheduleOnce(() => {
            this.isLock = false;
        }, 0.2);
        AppControll.Platform.ShowRewardVideoAd(this, (bool, str) => {
            if (bool) {
                AppControll.Platform.SendBuryPoint(100020);
                this.isLock = false;
                GameData.playerData.adChallengeCount -= 1;
                EvtManger.do(EventName.levelChallenge);
                GameData.SavePlayData();
                this.node.destroy();
            }
        })
    }
}


